package com.vodafone.nocs.inops.tools.fix;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.vodafone.nocs.inops.tools.fix.generated.Fix199Logic;
import com.vodafone.nocs.inops.tools.fix.generated.Fix199Logic.Bundle;
import com.vodafone.nocs.inops.tools.fix.generated.Fix199Logic.Bundle.Counter;
import com.vodafone.nocs.inops.tools.fix.generated.Fix199Logic.Bundle.Rules.Case;
import com.vodafone.nocs.inops.tools.fix.generated.Fix199Logic.Bundle.Rules.Case.Action;

public class XmlHelper {
	
	private Fix199Logic bundlesDef = null;
	private Map<String, Bundle> bundlesMap = null;


	public XmlHelper() {
		
		super();
		JAXBContext catesurepay;
		try {
			catesurepay = JAXBContext
					.newInstance("com.vodafone.nocs.inops.tools.fix.generated");
			Unmarshaller bundlesDefMarsh = catesurepay.createUnmarshaller();
			bundlesDef = (Fix199Logic) bundlesDefMarsh
					.unmarshal(new FileInputStream("FIDLogic.xml"));

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		bundlesMap = new HashMap<String, Bundle>();

		
		for (Bundle _b : bundlesDef.getBundle()) {
			bundlesMap.put(_b.getName(), _b);
		}
	}
	
	public Bundle getXmlBundle(String bundleId){
		
		return bundlesMap.get(bundleId);

	}

	public Tuple<Integer, String> getActionTuple(String bundleId, int issue) {
		
		Bundle _b = bundlesMap.get(bundleId);
		if (_b == null)
			return null;
		
		Tuple<Integer, String> _actions =  null;
		_actions = new Tuple<Integer, String>(0, "");

		if (_b.getRules() != null) {			

			for (Case _c : _b.getRules().getCase()) {

				if ((issue == UTIL.BUCKETSTARTDATEMISSING && _c.getType()
						.compareTo("bucketStartDateMissing") == 0)
						|| (issue == UTIL.BUCKETENDDATEMISSING && _c
								.getType().compareTo(
										"bucketEndDateMissing") == 0)
						|| (issue == UTIL.NOBUCKET && _c.getType()
								.compareTo("bucketMissing") == 0)
						|| (issue == UTIL.NOCOUNTER && _c.getType()
								.compareTo("counterMissing") == 0)
						|| (issue == UTIL.BUNDLEENDDATEMISSING && _c
								.getType().compareTo(
										"bundleEndDateMissing") == 0)) {
					for (Action _a : _c.getAction()) {
						if (_a.getType().compareTo("warning") == 0) {
							_actions.x = UTIL.WARNING + (Integer)_actions.x;
							_actions.y = _actions.y + UTIL.printFix(UTIL.WARNING);
						}
						if (_a.getType().compareTo("delete") == 0) {
							_actions.x = UTIL.DELETE + (Integer)_actions.x;
							_actions.y = _actions.y + UTIL.printFix(UTIL.DELETE);
						}
						if (_a.getType().compareTo("provision") == 0) {
							_actions.x = UTIL.PROVISION + (Integer)_actions.x;
							_actions.y = _actions.y + UTIL.printFix(UTIL.PROVISION);
						}
					}
				}

			}
		}		
		else{
			_actions.x = UTIL.CTOK;
			_actions.y = "";
		}
		return _actions;
	}

	public ArrayList<String> getCounterName(String bundleId) {

		UTIL.DEBLOG("getCounterName(String bundleId) " + bundleId);

		Bundle _b = bundlesMap.get(bundleId);
		if (_b == null)
			return null;
		
		ArrayList<String> array = null;
		
		if (_b.getName().compareTo(bundleId) == 0) {
			array = new ArrayList<String>();
			for (Counter _cs : _b.getCounter()) {
				array.add(_cs.getId());
			}
			return array;
		}

		if (array == null) {
			UTIL.OMLOG("!!!! Warning getCounterName(String bundleId) " + bundleId
					+ " not configured in XML");
		}

		return array;

	}

}
